<template>
	<view>
		就诊人
		<view class="patient">
			<u-upload :fileList="fileList6" @afterRead="afterRead" @delete="deletePic" name="6" multiple :maxCount="1"
				width="250" height="150">
				<image src="https://cdn.uviewui.com/uview/demo/upload/positive.png" mode="widthFix"
					style="width: 250px;height: 150px;"></image>
			</u-upload>
			<view class="patient_item">
				<view class="name">
					真实姓名
					<input type="text" v-model="from.name" placeholder="请输入姓名">
				</view>
				<view class="name">
					性别
					<picker style="margin-right: 130rpx;" @change="bindPickerChange" :value="index" :range="array">
						<view class="picker">
							当前选择：{{array[index]}}
						</view>
					</picker>
				</view>
				<view class="name">
					出生日期
					<picker mode="date" style="margin-right: 40rpx;" :value="date" @change="bindDateChange">
						<view class="picker">
							当前选择: {{date}}
						</view>
					</picker>
				</view>
				<view class="name">
					成员关系
					<picker style="margin-right: 130rpx;" @change="bindPickerChange1" :value="index1" :range="array1">
						<view class="picker">
							当前选择：{{array1[index1]}}
						</view>
					</picker>
				</view>
				<view class="name">
					身份证
					<input type="text" v-model="from.id_card" placeholder="请输入身份证">
				</view>
				<view class="name">
					手机号
					<input type="text" v-model="from.phone" placeholder="请输入手机号">
				</view>
			</view>
			<view class="foot">
				<button size="mini">取消</button>
				<button size="mini" @click="handlePatient" type="primary">确认添加</button>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		addPatient
	} from '../../utils/api.js'
	export default {
		data() {
			return {
				date: '2023-03-28',
				array: ['男', '女', '保密'],
				index: 0,
				array1: ['父母', '子女', '自己', '其他'],
				index1: 0,
				fileList6: [],
				from: {
					name: "",
					sex: "",
					born: '',
					relation: '',
					id_card: '',
					phone: ''
				}
			};
		},
		methods: {
			// 新增就诊人
			handlePatient() {
				addPatient(this.from).then(res => {
					console.log(res);
					uni.navigateTo({
						url: '/pages/patientMan/patientMan'
					})
				})
			},
			// 获取日期
			bindDateChange: function(e) {
				console.log('picker发送选择改变，携带值为', e)
				this.date = e.detail.value
				this.from.born = e.detail.value
			},
			// 获取性别
			bindPickerChange: function(e) {
				console.log('picker发送选择改变，携带值为90', e)
				this.index = e.detail.value
				this.from.sex = this.array[e.detail.value]
			},
			// 成员关系
			bindPickerChange1: function(e) {
				console.log('picker发送选择改变，携带值为', e)
				this.index = e.detail.value
				this.from.relation = this.array1[e.detail.value]
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'http://192.168.2.21:7001/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							setTimeout(() => {
								resolve(res.data.data)
							}, 1000)
						}
					});
				})
			},
		}
	}
</script>

<style lang="scss">
	.patient {
		width: 750rpx;

	}

	.data-v-69e2a36e {
		margin-left: 40rpx;
	}

	.patient_item {
		width: 750rpx;
		margin-top: 40rpx;
	}

	.name {
		width: 750rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		height: 80rpx;
		text-indent: 1rem;
		border-bottom: 4rpx solid #ddd;
	}

	.foot {
		width: 750rpx;
		display: flex;
		align-items: center;
		justify-content: space-around;
		margin-top: 200rpx;
	}

	button {
		width: 80%;
		height: 80rpx;
	}
</style>